<?php
/**
 *  创建微信投票类
 *
 * */

namespace app\admin\controller;
use think\Db;

class Activity extends Base
{
    protected $model;
    protected $modelAttached;

    protected function initialize()
    {
        parent::initialize();
        $this->model = \app\common\model\Activity::getInstance();
        $this->modelAttached = \app\common\model\ActivityAttached::getInstance();
    }

    /**
     * 添加活动页面
     *
     * */
    public function addTpl()
    {

        return $this->fetch('addtpl');
    }

    /**
     * 添加活动操作方法
     *
     * */
    public function publish()
    {
        if ($this->request->isPost()) { // 操作
            $post = $this->request->param();
            // 测试数据
            $post = [
                'title' => '测试投票',
                'apply_start_time' => '1548916622',
                'apply_end_time' => '1551508622',
                'vote_start_time' => '1548916622',
                'vote_end_time' => '你好',
            ];
            $id = isset($post['id']) ? intval($post['id']) : 0;
            // 开启事物
            $this->modelAttached->startTrans();
            try {
                if ($id > 0) { // 修改
                    $res = $this->model->editData($post, $id);
                    // 测试数据
                    $post_one = [
                        'activity_id' => $res,
                        'ssss' => '1111',
                    ];
                    $this->modelAttached->editData($post_one);
                } else { // 添加
                    $res = $this->model->addData($post);
                    // 测试数据
                    $post_one = [
                        'activity_id' => $res,
                        'ssss' => '1111',
                    ];
                    $this->modelAttached->inster($post_one);
                }
                $this->modelAttached->commit();
                $this->success('操作成功', url('listTpl'));
            } catch (\Exception $e) {
                $this->modelAttached->rollback();
                // 捕获异常
                dump($e->getMessage());
                $this->error('操作失败');
            }
        } else { // 展示

            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            $info = [];
            if ($id > 0) {
                $info = $this->model->getOne($id);
            }
            $this->assign($info);
            return $this->fetch();
        }
    }

    /**
     * 活动列表页面
     *
     * */
    public function listTpl()
    {
        $post = $this->request->param();
//        $admin_id = $this->admin_id;
        $admin_id = 1;
        // 判断这个用户是否是超级管理员  $super 1 为超级管理员，2为管理员，3为用户
        // 先定义为超管理员
        $super = 1;
        $where = [];

        // 查询当前
        $top_time = isset($post['top_time']) ? $post['top_time'] : 1;
        $keyword = isset($post['keyword']) ? $post['keyword'] : '';
        $start_id = isset($post['start_id']) ? $post['start_id'] : 0;
        $type_time = isset($post['type_time']) ? $post['type_time'] : 1;
        $button_time = isset($post['button_time']) ? $post['button_time'] : '';
        $filed_sort = isset($post['filed']) ? $post['filed'] : '';
        $can_order = isset($post['order']) ? $post['order'] : '';

        if ($start_id == 1) { // 结束时间
            $field = 'a.end_time';
        } else {// 开始时间
            $field = 'a.start_time';
        }
        switch ($type_time) {
            case 2: // 昨天
                $start_time = date('Y-m-d', strtotime('-1 day', time()));
                $end_time = date('Y-m-d', time());
                break;
            case 3: // 今天
                $start_time = date('Y-m-d', time());
                $end_time = date('Y-m-d', strtotime('+1 day', time()));
                break;
            case 4: // 明天
                $start_time = date('Y-m-d', strtotime('+1 day', time()));
                $end_time = date('Y-m-d', strtotime('+2 day', time()));
                break;
            case 5: // 后天
                $start_time = date('Y-m-d', strtotime('+2 day', time()));
                $end_time = date('Y-m-d', strtotime('+3 day', time()));
                break;
        }
        if ($type_time > 1) {
            $where[] = [$field, 'between time', [$start_time, $end_time]];
        }
        if (!empty($button_time)) {
            $exprie_time = explode(' - ', $button_time);
            $start_time = $exprie_time[0];
            $end_time = date('Y-m-d', strtotime("+1 day", strtotime($exprie_time[1])));
            $where[] = [$field, 'between time', [$start_time, $end_time]];
        }
        if (!empty($keyword)) {
            if (is_numeric($keyword)) { // 搜索的为序号或者ID
                if ($super == 1) {// id
                    $where[] = ['a.id', '=', $keyword];
                } else { // 序号
                    $where[] = ['a.serial', '=', $keyword];
                }
            } else { // 名称
                $where[] = ['a.title', 'like', "%{$keyword}%"];
            }
        }
        if ($top_time > 1) {
            switch ($top_time) {
                case 2: // 报名中
                    $where[] = ['a.apply_start_time', '<', time()];
                    $where[] = ['a.apply_end_time', '>', time()];
                    break;
                case 3: // 进行中
                    $where[] = ['a.start_time', '<', time()];
                    $where[] = ['a.end_time', '>', time()];
                    break;
                case 4: // 已结束
                    $where[] = ['a.end_time', '<', time()];
                    break;
                case 5: // 未开始
                    $where[] = ['a.start_time', '>', time()];
                    break;
            }
            $where[] = ['a.status', '=', 0];
        }
        $where[] = ['a.isdelete', '=', 0];
        $where[] = ['a.admin_id', '=', $admin_id];
        // 排序
        $order = "a.id desc";
        if (!empty($filed_sort)) {
            if ($filed_sort == 'serial' && $super == 1) { // 超级管理员
                $order = "a.id ".$can_order;
            } elseif ($filed_sort == 'vote_num') { // 总投票数
                $order = $filed_sort . ' ' . $can_order;
            } elseif ($filed_sort == 'gift_price') { // 总礼物价格
                $order = $filed_sort . ' ' . $can_order;
            } elseif ($filed_sort == 'visitor_num') { // 总浏览量
                $order = $filed_sort . ' ' . $can_order;
            } elseif ($filed_sort == 'competitor_number') { // 选手数量
                $order = $filed_sort . ' ' . $can_order;
            } else {
                $order = "a." . $filed_sort . ' ' . $can_order;
            }
        }
        $count = $this->model->alias('a')->where($where)->count('a.id');
        $fields = " a.*,count(b.id) as competitor_number,
                    a.false_vote_num + a.true_vote_num as vote_num,
                    a.real_visitor_num + a.sham_visitor_num as visitor_num,
                    a.false_gift_price + a.true_gift_price as gift_price";
        $list = $this->model->alias('a')->field($fields)->leftJoin('zzcms_competitor b','a.id=b.activity_id')->where($where)->order($order)->group('b.activity_id')->select();
        $status_tit = '';
        foreach ($list as $k => &$v) {
            if ($v['vote_start_time'] < time()) {
                $status_tit = '<span class="span1">投票中</span>';
            }
            if ($v['apply_start_time'] < time()) {
                $status_tit = '<span class="span1">报名中</span>';
            }
            if ($v['apply_start_time'] < time() && $v['vote_start_time'] < time()) {
                $status_tit = '<span class="span1">报名投票中</span>';
            }
            if ($v['start_time'] > time()) {
                $status_tit = '<span class="span3">等待开始</span>';
            }
            if ($v['end_time'] < time()) {
                $status_tit = '<span class="span2">已结束</span>';
            }
            if ($v['status'] == 1) {
                $status_tit = '<span class="span2">关闭</span>';
            }
            $v['status_tit'] = $status_tit;
        }
        if($can_order == 'asc'){
            $arrow = 'fa-long-arrow-up';
        }else{
            $arrow = 'fa-long-arrow-down';
        }
        $parame = [
            'top_time' => $top_time,
            'super' => $super,
            'keyword' => $keyword,
            'start_id' => $start_id,
            'type_time' => $type_time,
            'button_time' => $button_time,
            'arrows_order' => $arrow,
            'filed_sort' => $filed_sort,
        ];
        $this->assign("list", $list);
        $this->assign("count", $count);
        $this->assign("parame", $parame);
        return $this->fetch('listtpl');
    }

    // 增加浏览量
    public function visitorAction()
    {
        $post = $this->request->param();
        $num = isset($post['num']) ? $post['num'] : 0;
        $id = isset($post['id']) ? $post['id'] : 0;
        if ($id <= 0) {
            return $this->error('增加失败');
        }
        $res = $this->model->where(['id' => $id])->setInc('sham_visitor_num', $num);
        if ($res) {
            return $this->error('增加成功');
        } else {
            return $this->error('增加失败');
        }
    }


    // 复制添加
    public function copyAction()
    {
//        $this->model

    }


}